package cn.itcast.service.impl;

import cn.hutool.core.convert.Convert;
import cn.itcast.dao.MemberDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据月份统计会员数量
    public List<Integer> findMemberCount(List<String> list) {
        List<Integer> memberCount = new ArrayList<>();
        //遍历月份
        for (String month : list) {
            month = month + ".31";//2021.10.31  即每个月1号到31号的会员数
            Integer count = memberDao.findMemberCountDate(month);
            memberCount.add(count);
        }
        return memberCount;
    }

    //根据身份证查询会员对象  来判断是否重复下单
    @Override
    public Member findByIdCard(String idCard) {
        return this.memberDao.findByIdCard(idCard);
    }



    //添加一个会员数据
    @Transactional
    @Override
    public Integer insert(Member member) {
        return  this.memberDao.insert(member);

    }


//=========================后端会员管理====================================

    /**
     * 档案号查询
     *
     * @param fileNumber
     * @return
     */
    @Override
    public List<Member> findFileNumberById(String fileNumber) {
        return memberDao.findfileNumberById(fileNumber);
    }

    /**
     * 姓名查询
     *
     * @param name
     * @return
     */
    @Override
    public List<Member> findNameAll(String name) {
        return memberDao.findNameAll(name);
    }

    /**
     * 手机号
     *
     * @param phoneNumber
     * @return
     */
    @Override
    public List<Member> findMemberByPhone(String phoneNumber) {
        return memberDao.findMemberByPhone(phoneNumber);
    }

    /**
     * 添加信息
     *
     * @param member
     * @return
     */
    @Override
    public Boolean save(Member member) {
        //查询该档案号是否重复
        if (memberDao.findfileNumberById(member.getFileNumber()).size() > 0
                || memberDao.findMemberByPhone(member.getPhoneNumber()).size() > 0) {
            return false;
        }
        memberDao.save(member);
        return true;
    }

    /**
     * 更新会员信息
     *
     * @param member
     */
    @Override
    public void update(Member member) {
        memberDao.update(member);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据条件分页查询检查项信息
        Page<Member> page = memberDao.findAll();
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    /**
     * 根据用户输入信息查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findNameOrPhoneOrFileNumber(QueryPageBean queryPageBean) {
        if (queryPageBean.getCurrentPage()!=0){
            queryPageBean.setCurrentPage(0);
        }
        if (memberDao.findNameAll(queryPageBean.getQueryString()).size() > 0) {

            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            List<Member> nameAll = memberDao.findNameAll(queryPageBean.getQueryString());

            return new PageResult(Convert.toLong(nameAll.size()), nameAll);
        } else if (memberDao.findfileNumberById(queryPageBean.getQueryString()).size() > 0) {

            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            List<Member> members = findFileNumberById(queryPageBean.getQueryString());

            return new PageResult(Convert.toLong(members.size()), members);
        } else if (memberDao.findMemberByPhone(queryPageBean.getQueryString()).size() > 0) {

            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            List<Member> phoneById = findMemberByPhone(queryPageBean.getQueryString());

            return new PageResult(Convert.toLong(phoneById.size()), phoneById);
        }
        return null;
    }

    /**
     * 权限状态
     *
     * @param userid
     * @param status
     */
    @Override
    public void updataStatus(String userid, Integer status) {
        memberDao.updataStatus(userid, status);
    }
}
